package www.ym.com.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import www.ym.com.common.CustomException;
import www.ym.com.common.R;
import www.ym.com.common.RL;
import www.ym.com.entity.Account;
import www.ym.com.entity.AccountLog;
import www.ym.com.entity.User;
import www.ym.com.service.AccountLogService;
import www.ym.com.service.AccountService;
import www.ym.com.service.UserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Slf4j
@RestController
@RequestMapping("/log")
public class AccountLogController {

    @Autowired
    private AccountLogService accountLogService;

    @Autowired
    private AccountService accountService;

    @Autowired
    private UserService userService;

    /**
     * 银行账户金额调整
     * @param accountLog
     * @return
     */
    @PostMapping("menoyType")
    public R<String> addMenoy(@RequestBody AccountLog accountLog) {

        //查询账户信息
        LambdaQueryWrapper<Account> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Account::getId, accountLog.getBankId());
        Account one = accountService.getOne(wrapper);

        //查询用户信息
        User user= userService.getById(accountLog.getUserId());
        String msg="";

        switch (accountLog.getType()) {
            //入金操作
            case "0":
                one.setKyMenoy(one.getKyMenoy() + accountLog.getTradeAmt());
                msg="入账成功";
                break;
            //出金操作
            case "1":
                if(one.getKyMenoy()<accountLog.getTradeAmt()){
                    throw  new CustomException("可用金额不足，处理失败");
                }
                one.setKyMenoy(one.getKyMenoy() - accountLog.getTradeAmt());
                msg="出账成功";
                break;
            //冻结
            case "2":
                one.setDjMenoy(one.getDjMenoy()+accountLog.getTradeAmt());
                one.setDjMenoy(one.getKyMenoy()-accountLog.getTradeAmt());
                msg="冻结操作成功";

                break;
            //解冻
            case "3":
                if(one.getDjMenoy()<accountLog.getTradeAmt()){
                    throw  new CustomException("冻结金额不足，处理失败");
                }
                one.setKyMenoy(one.getKyMenoy()+accountLog.getTradeAmt());
                one.setDjMenoy(one.getDjMenoy()-accountLog.getTradeAmt());
                msg="解冻操作成功";
                break;
        }
        accountLog.setAccountName(one.getBankName());
        accountLog.setOpName(user.getUsername());
        //总金额=可用金额+冻结金额
        one.setTotalMenoy(one.getKyMenoy() + one.getDjMenoy());

        accountService.updateById(one);
        accountLogService.save(accountLog);
        return R.success("",msg);
    }

    /**
     * 交易明细
     * @param jsonObject
     * @return
     */
    @RequestMapping("/list")
    public R<Object> List(@RequestBody JSONObject jsonObject){
        Page<AccountLog> list = accountLogService.logList(jsonObject);
        List<AccountLog> records = list.getRecords();
        log.info("交易明细"+records);
        long total = list.getTotal();
        long pageNum = list.getCurrent();
        long pageSize = list.getSize();

        //用户id
        Long bankId =Long.valueOf((String.valueOf(jsonObject.get("bankId"))));
        Account byId = accountService.getById(bankId);
        Integer totalMenoy = byId.getTotalMenoy();
        Integer kyMenoy = byId.getKyMenoy();
        Integer djMenoy = byId.getDjMenoy();

        Map<String, Object> object = new HashMap<>();
        object.put("records",records);
        object.put("total",total);
        object.put("pageNum",pageNum);
        object.put("pageSize",pageSize);
        object.put("totalMenoy",totalMenoy);
        object.put("kyMenoy",kyMenoy);
        object.put("djMenoy",djMenoy);

        return R.success(object,"处理成功");
    }


}
